﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>行编辑ComboBox联动 </title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <link href="../demo.css" rel="stylesheet" type="text/css" />

    
    <script src="../../scripts/boot.js" type="text/javascript"></script> 
    
    <!--引入皮肤样式-->
    <link href="../../scripts/miniui/themes/blue/skin.css" rel="stylesheet" type="text/css" />

</head>
<body>
    <h1>行编辑ComboBox联动</h1>
    
    <div style="width:800px;">
        <div class="mini-toolbar" style="border-bottom:0;padding:0px;">
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" iconCls="icon-edit" onclick="editRow()" plain="true">编辑</a><span class="separator"></span><a class="mini-button" iconCls="icon-add" onclick="addRow()" plain="true">增加</a>
                        <a class="mini-button" iconCls="icon-remove" onclick="removeRow()" plain="true">删除</a>
                        <span class="separator"></span>
                        <a class="mini-button" iconCls="icon-save" onclick="saveData()" plain="true">保存</a>            
                    </td>
                    <td style="white-space:nowrap;">
                       
                    </td>
                </tr>
            </table>           
        </div>
    </div>
    <div id="datagrid1" class="mini-datagrid" style="width:800px;height:280px;" 
       url="../data/AjaxService.jsp?method=SearchEmployees" multiSelect="true"
    >
        <div property="columns">
            <div type="checkcolumn"></div>
            <div field="loginname" width="120" headerAlign="center" allowSort="true">员工帐号
                <input property="editor" class="mini-textbox" style="width:100%;"/>
            </div>                   
            <div name="Dept" field="dept_id" displayField="dept_name" width="150" headerAlign="center" >部门
                <input property="editor" class="mini-combobox" style="width:100%;" 
                    valueField="id" textField="name" url="../data/AjaxService.jsp?method=GetDepartments"
                    onvaluechanged="onDepartChanged"
                    />                
            </div>   
            <div name="Position" field="position"  displayField="position_name"  width="150" headerAlign="center" >职位
                <input property="editor" class="mini-combobox" style="width:100%;" 
                    valueField="id" textField="name"
                />                
            </div>   
        </div>
    </div>
    <script type="text/javascript">
        var Genders = [{ id: 1, text: '男' }, { id: 2, text: '女'}];

        mini.parse();

        var grid = mini.get("datagrid1");
        grid.load();

        ////////////////////////////
        function editRow() {
            var rows = grid.getSelecteds();

            if (rows.length > 0) {
            
                for (var i = 0, l = rows.length; i < l; i++) {
                    var row = rows[i];
                    grid.beginEditRow(row);
                }
            } 
        }
        function addRow() {
            var newRow = { name: "New Row" };
            grid.addRow(newRow, 0);
            grid.beginEditRow(newRow);
        }
        function removeRow() {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                grid.removeRows(rows, true);
            }
        }
        function saveData() {
            grid.commitEdit();

            var data = grid.getChanges();
            var json = mini.encode(data);

            grid.loading("保存中，请稍后......");
            $.ajax({
                url: "../data/AjaxService.jsp?method=SaveEmployees",
                data: { data: json },
                type: "post",
                success: function (text) {
                    grid.reload();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });
        }
        ////////////////////////
        //选择“部门”，设置“职位”
        function onDepartChanged(e) {            
            var combo = e.sender;
            var row = grid.getEditorOwnerRow(combo);
            var editor = grid.getCellEditor("Position", row);

            var id = combo.getValue();
            var url = "../data/AjaxService.jsp?method=GetPositionsByDepartmenId&id=" + id
            editor.setUrl(url);
            editor.setValue("");
        }
        //“职位”combo的url在cellbeginedit的时候，根据前面值自动设置
        grid.on("cellbeginedit", function (e) {
            if (e.field == "position") {
                var editor = e.editor;
                var id = e.record.dept_id;
                var url = "../data/AjaxService.jsp?method=GetPositionsByDepartmenId&id=" + id
                editor.setUrl(url);
            }
        });

    </script>

    <div class="description">
        <h3>Description</h3>
        <p>部门和职位联动选择</p>
    </div>
</body>
</html>